<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/16
 * Time: 11:41
 */

namespace app\shop\model;


use think\Model;

class ArticleModel extends Model
{
    protected $table = 'dp_article';
    protected $pk = 'id';
    protected $field = true;


    public static function getAllList()
    {
        return self::paginate(10);
    }

    public static function getList($where, $p=1, $row=10)
    {
        $list = self::where($where)
            ->where('is_show', 1)
            ->limit( ($p-1) * $row, $p*$row )
            ->field('id,cate_id,title,author,desc,original_img,time,tags')
            ->select();
        $list = getImagePath($list, 'original_img');
        $list = self::tagToArr($list);
        return $list;
    }

    public static function getOne($id)
    {
        return self::where('id', $id)->find();
    }

    public static function store($data, $id=0)
    {
        $admin = session('user_auth');
        if($id == 0){
            $data['time'] = time();
            $data['admin_id'] = $admin['uid'];
            $data['action_time'] = time();
            return self::insert($data);
        }else{
            $data['admin_id'] = $admin['uid'];
            $data['action_time'] = time();
            return self::where('id', $id)->update($data);
        }
    }

    public static function del($id)
    {
        return self::where('id', $id)->delete();
    }

    private static function tagToArr($list)
    {
        foreach ($list as $k => $item){
            $list[$k]['tags'] = explode(',', $item['tags']);
        }
        return $list;
    }
}